{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "23aea41c",
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "import cv2\n",
    "from PIL import Image"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "0f8ea6be",
   "metadata": {},
   "outputs": [],
   "source": [
    "def resize_img(DATADIR, data_k, img_size):\n",
    "    w = img_size[0]\n",
    "    h = img_size[1]\n",
    "    path = os.path.join(DATADIR, data_k)\n",
    "    #返回path路径下所有文件的名字，以及文件夹的名字，\n",
    "    img_list = os.listdir(path)\n",
    "    \n",
    " \n",
    "    for i in img_list:\n",
    "        if i.endswith('.jpg'):\n",
    "            # 调用cv2.imread读入图片，读入格式为IMREAD_COLOR\n",
    "            img_array = cv2.imread((path + '/' + i), cv2.IMREAD_COLOR)\n",
    "            # 调用cv2.resize函数resize图片\n",
    "            new_array = cv2.resize(img_array, (w, h), interpolation=cv2.INTER_CUBIC)\n",
    "            img_name = str(i)\n",
    "            '''生成图片存储的目标路径'''\n",
    "            save_path = path + '_new/'\n",
    "            if os.path.exists(save_path):\n",
    "                print(i)\n",
    "                '''调用cv.2的imwrite函数保存图片'''\n",
    "                save_img=save_path+img_name\n",
    "                cv2.imwrite(save_img, new_array)\n",
    "            else:\n",
    "                os.mkdir(save_path)\n",
    "                save_img = save_path + img_name\n",
    "                cv2.imwrite(save_img, new_array)\n",
    " "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "5f468961",
   "metadata": {},
   "outputs": [],
   "source": [
    "def image_binarization(DATADIR, data_k):\n",
    "        # 将图片转为灰度图\n",
    "    path = os.path.join(DATADIR, data_k)\n",
    "    #print(path)\n",
    "    \n",
    "    img_list = os.listdir(path)\n",
    "    for image_file in img_list:\n",
    "        img = Image.open(DATADIR+'\\\\'+data_k+'\\\\'+image_file)\n",
    "        img=img.convert('1')\n",
    "        \n",
    "        #ray = cv2.cvtColor(image_file, cv2.COLOR_BGR2GRAY)\n",
    "            # retval, dst = cv2.threshold(gray, 110, 255, cv2.THRESH_BINARY)\n",
    "            # 最大类间方差法(大津算法)，thresh会被忽略，自动计算一个阈值\n",
    "        #etval, dst = cv2.threshold(gray, 127, 255, cv2.THRESH_BINARY)\n",
    "        \n",
    "        \n",
    "\n",
    "        save_path = path + '_binary'\n",
    "        bin_file=os.path.splitext(image_file)[0]\n",
    "        if os.path.exists(save_path):\n",
    "            #rint(i)\n",
    "            img.save(save_path+'\\\\'+bin_file+'.png')\n",
    "        else:\n",
    "            os.mkdir(save_path)\n",
    "            img.save(save_path+'\\\\'+bin_file+'.png')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d6b997f9",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
